<?php

    #-------------------------------------------------------------#
	# This file is part of the Tekuna MVC Microframework for PHP. #
	# (c) 2009 Niels Jäckel <niels.jaeckel@tekuna.org>            #
	# Please visit http://tekuna.org/ for more information.       #
	#                                                             #
	# This software is licensed under the MIT License. Please     #
	# view the LICENSE file for the full text of this license.    #
    #-------------------------------------------------------------#


	Tekuna :: load('org.tekuna.core.mvc.TekunaController');
	Tekuna :: load('org.tekuna.core.request.Request');
	Tekuna :: load('org.tekuna.core.response.Response');
	Tekuna :: load('org.tekuna.core.request.URLInformation');
	Tekuna :: load('org.tekuna.core.request.ClientInformation');
	Tekuna :: load('org.tekuna.core.mvc.controller.ControllerURLException');


	/**
	 * Basic TekunaController implementation that supports only the
	 * necessary interface implementation and object storage.
	 *
	 * Use this controller for subclassing, if you want to write the
	 * generation process completely on your own, e.g. for image or PDF
	 * creations. For XHTML outputs it is recommendet to subclass from
	 * the AbstractMVCController.
	 */

	abstract class AbstractController implements TekunaController {

		protected $objContext = NULL;
		
		
		/**
		 * This array defines the names and the count of required URL parts. The pattern
		 * * / *.html for example defines two matchable parts that could be called "folder"
		 * and "file". For this case, the array would be array('folder', 'file')
		 * 
		 * This value must be overwritten to adjust the facilities of the controller.
		 */
		 
		 protected $arrURLParts = array();


		/**
		 * Constructor definition for controllers. The application context
		 * is given and is stored internally to be used in the getOutput()
		 * method. Finally the validateURLParts() method is called that 
		 * possibly modifies the entries of the URL parts.
		 *
		 * @param TekunaApplicationContext $objContext
		 */

		public function __construct(TekunaApplicationContext $objContext) {

			$this -> objContext = $objContext;
			
			$this -> validateURLParts();
		}
		
		
		/**
		 * This method validates and renames the matched URL parts in the URLInformation of the
		 * application context. If the count of matched URL parts is not equal to the defined
		 * part count in the member $arrURLParts, a ControllerURLException is thrown.
		 * 
		 * If you want to implement a controller that accepts a variable count of URL parts this 
		 * method should be overwritten.
		 */
		 
		protected function validateURLParts() {
			
			$objURLInformation = $this -> getApplicationContext() -> getURLInformation();
			$arrParts = $objURLInformation -> getMatchedURLParts();
			
			// check the parts count
			$sRequiredCount = count($this -> arrURLParts);
			$sGivenCount = count($arrParts);
			if ($sGivenCount != $sRequiredCount) {
				
				$sClassName = get_class($this);
				$sRequiredParts = implode(', ', $this -> arrURLParts);
				$sGivenParts = implode(', ', $arrParts);
				throw new ControllerURLException("The class '$sClassName' requires $sRequiredCount URL parts ($sRequiredParts). $sGivenCount available ($sGivenParts).");
			}

			// store named URL parts if necessary
			if ($sRequiredCount > 0) {

				$arrNamedParts = array_combine($this -> arrURLParts, $arrParts);
				$objURLInformation -> setMatchedURLParts($arrNamedParts);
			}
		}


		/**
		 * @return TekunaApplicationContext returns the application context object.
		 */

		public function getApplicationContext() {

			return $this ->	objContext;
		}
	}

